using RestSharp;

namespace HavardFjaer.Pushover.Client
{
    public class PushoverClientBase
    {
        protected readonly RestRequest RestRequest;

        public PushoverClientBase(string token, string user, string endpoint, ResponseFormat responseFormat)
        {
            Token = token;
            User = user;
            Endpoint = endpoint;

            RestRequest = new RestRequest(Endpoint, Method.POST);
            RestRequest.AddUrlSegment("format", responseFormat == ResponseFormat.Json ? "json" : "xml");
            RestRequest.AddParameter("token", Token);
            RestRequest.AddParameter("user", User);
        }

        public string Token { get; set; }
        public string User { get; set; }
        public string Endpoint { get; set; }

        protected void AddParameterIfNotNull(string name, string value)
        {
            if (value != null)
                RestRequest.AddParameter(name, value);
        }

        protected string SendMessage()
        {
            var client = new RestClient();
            return client.Execute(RestRequest).Content;
        }
    }
}